package com.zan.completable;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @Author Zan
 * @Create 2024/3/1 16:39
 * @Description :
 */
public class CompletableFutureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 异步调用 没有返回值的方法 runAsync
        CompletableFuture<Void> completableFuture1 = CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "CompletableFuture1");
        });
        completableFuture1.get();

        // 异步调用 有返回值的方法 supplyAsync
        CompletableFuture<Integer> completableFuture2 = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "CompletableFuture2");
            // 模拟异常
            int i = 1 / 0;
            return 1024;
        });
        completableFuture2.whenComplete((t, u) -> {
            System.out.println("----t=" + t); // 方法返回值
            System.out.println("----u=" + u); // 得到的异常信息
        }).get();
    }
}
